package com.yulebaby.callsenter.utils;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.yulebaby.callsenter.R;


public class DialogUtils {
    public interface DialogConfirmCallback {
        void onConfirm(DialogInterface dialog);
    }

    public interface DialogCancelCallback {
        void onCancel(DialogInterface dialog);
    }

    private static Toast mToast = null;
    private static AlertDialog show;

    public static void showMessage(Context context, String msg) {
        if (mToast != null) {
            mToast.cancel();
            mToast = null;
        }
        mToast = new Toast(context);
        LayoutInflater inflate = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflate.inflate(R.layout.message_view, null);
        TextView tv = (TextView) v.findViewById(R.id.msg);
        tv.setText(msg);
        mToast.setView(v);
        mToast.setGravity(Gravity.CENTER, 0, 0);
        mToast.setDuration(Toast.LENGTH_SHORT);
        mToast.show();

    }

    public static void ShowConfirmDialog(Context context, String msg,
                                         final DialogConfirmCallback cb) {
        if (((Activity) context).isFinishing()) {
            return;
        }
        new AlertDialog.Builder(context)
                .setMessage(msg)
                .setPositiveButton(R.string.confirm,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                if (cb != null) {
                                    cb.onConfirm(dialog);
                                }

                            }
                        })
                .setCancelable(false).show();
    }

    public static void ShowConfirmCancelDialog(Context context, String title, String msg, String btnLeft_msg, String btnRight_msg, final DialogConfirmCallback confirmCB,
                                               final DialogCancelCallback cancelCB) {

        if (((Activity) context).isFinishing()) {
            return;
        }
//        LayoutInflater inflate = (LayoutInflater) context
//                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//        View v = inflate.inflate(R.layout.dialog_toseasoon, null);
        new AlertDialog.Builder(context)
                .setTitle(title)
//                .setView(v)
                .setMessage(msg)
                .setPositiveButton(btnRight_msg,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {

                                if (confirmCB != null) {
                                    confirmCB.onConfirm(dialog);
                                }
                            }
                        })
                .setNegativeButton(btnLeft_msg, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {


                        if (cancelCB != null) {
                            cancelCB.onCancel(dialog);
                        }
                    }
                }).setCancelable(false).show();


    }


    public static void ShowNetworkErrorDialog(final Context context,
                                              final DialogConfirmCallback cb) {
        if (((Activity) context).isFinishing()) {
            return;
        }
        new AlertDialog.Builder(context)
                .setMessage(R.string.net_isonline_tip_msg)
                .setPositiveButton(R.string.confirm,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                if (cb != null) {
                                    cb.onConfirm(dialog);
                                }

                            }
                        }).setCancelable(false).show();
    }

    public static ProgressDialog showProgressDlg(Context context, String msg) {
        ProgressDialog dlg = null;
        if (msg == null) {
            msg = context.getString(R.string.wait);
        }
        try {
            dlg = new ProgressDialog(context);
            dlg.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            dlg.setMax(100);
            dlg.setMessage(msg);
            dlg.setCancelable(false);
            dlg.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dlg;
    }

}
